<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> -->
    <script src="../node_modules/vue/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">
        <table border="1" cellspacing="0" cellpadding="20">
            <tr v-for="item in shopCarArr" :key="item.id">
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button @click="item.num++" :disabled="item.num==item.store">+</button>
                    {{item.num}}
                    <button @click="sub(item)">-</button>
                </td>
                <td>{{item.price*item.num}}元</td>
            </tr>
        </table>
    </div>
</body>

</html>
<script>
    const { createApp } = Vue;
    const vm = createApp({
        data() {
            return {
                message1: 'Hello',
                shopCarArr: [
                    { id: 1, name: '三国演义', price: 100, num: 2, store: 5 },
                    { id: 2, name: '水浒转', price: 200, num: 1, store: 5 },
                    { id: 3, name: '红楼梦', price: 300, num: 3, store: 5 }
                ]
            }
        },
        methods: {
            sub(item) {
                console.log(item);
                item.num--
                console.log(item.num);
                if (item.num == 0) {
                    // 要进行删除操作
                    // this.shopCarArr.splice(index,1)
                    this.shopCarArr.forEach((v, i) => {
                        if (v.id == item.id) {
                            this.shopCarArr.splice(i, 1)
                        }
                    })
                }
            }
        }
    }).mount('#app') 
</script>